<?php
try{
    include '../ini.php';
    include '../classes/xl_product_categories.php';
    $dt_xl_product_category=new xl_product_category;
    if(empty($_POST['data'])){
        throw new Exception('Vui lòng chọn mã để xóa');
    }
    $dbh->beginTransaction();
    foreach($_POST['data'] as $k=>$id){
        $list_category=$dt_xl_product_category->delete_all($id);
        if($k != $list_category['id']){
            throw new Exception('Đã xảy ra lỗi quá trình xử lí');
        }
        /*Kiểm tra quá trình liệt kê danh sách*/
        if($list_category === FALSE){
            throw new Exception('Đã xảy ra sự cố trong quá trình xóa');
        }
        $list_products=$dt_xl_product_category->read_product($id);
         
        if($list_products != NULL && $list_products === FALSE){
            throw new Exception('Đã xảy ra sự cố trong quá trình xóa');
        }
        //$read_parent=$dt_xl_product_category->read_id_parent($_POST['data'][$id]);
        if($list_category['banner']!='' OR $list_category['banner']==NULL){
             $category=$dt_xl_product_category->delete($id);
        }
        //$time_folder=date_format(date_create($list_category['day_time']),'m-Y');
        $day_time_category=image('product_category',date_format(date_create($list_category['day_time']),'m-Y'));
        if($category === FALSE){
            throw new Exception('Đã xảy lỗi ra quá trình xóa');
        } else {
             if($list_category['banner']!=''){
	               unlink($day_time_category.'/'.$list_category['banner']);
             }
        }
       
        if($list_products !=  NULL){
            foreach($list_products as $detail_product){
                $day_time_product=image('product',date_format(date_create($detail_product['day_time']),'m-Y'));
                
                if($detail_product['image']!=''){
	               unlink($day_time_product.'/'.$detail_product['image']);
                }
                $list_image=$dt_xl_product_category->delete_all_product_images($detail_product['id']);
                foreach($list_image as $image){
                    $day_time_list_image=image('product',date_format(date_create($image['product_day_time']),'m-Y'));
                    if($image['image']!='' OR $image['image']==NULL){
                        $product_image=$dt_xl_product_category->delete_product_images($image['product_id']);
                    }
                    if($product_image === false){
                        throw new Exception('Xảy ra lỗi trong quá trình xóa bài viết');
                    }
                    if($image['image']!=''){
                        unlink($day_time_list_image.'/'.$image['image']);
                    }
                }
                
            }
            foreach($list_products as $detail_product){
                if($detail_product['image']!='' OR $detail_product['image']==NULL){
                         $product=$dt_xl_product_category->delete_product($detail_product['category_id']);
                }
                if($product === FALSE){
                    throw new Exception('Đã xảy lỗi ra quá trình xóa');
                } 
            }
        }
    }
 
    $dbh->commit();
    $dbh=NULL;
    $_SESSION['msg']='Thành Công';
    $_SESSION['type_msg']='success';
    header('Location: categories_list.php');
    
} catch(Exception $e){
    $dbh=NULL;
    $_SESSION['msg']=$e->getMessage();;
    $_SESSION['type_msg']='error';
    if(empty($_SERVER['HTTP_REFERER'])){
        header('Location: categories_list.php');
    } else {
        header('Location:'.$_SERVER['HTTP_REFERER']);
    }
    
}
